﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfUMLTry1.Model;

namespace WpfUMLTry1.ViewModel
{
    class Fight_VM : VM_Base
    {
        #region private
        #region logical
        private Model.gameModel _model;
        private PlayerCharacter _ply;
        private AbstractCreature _mob;
        #endregion
        #region VM datas
        private Boolean _PlayerAttackEnable;
        #endregion
        #endregion
        
        #region Public - most for VM binding

        public Boolean PlayerAttackEnable { get { return _PlayerAttackEnable; } set { _PlayerAttackEnable = value; OnPropertyChanged("PlayerAttackEnable"); } }

        public String PlayerName { get { return _ply.name; } }
        public Int32 PlayerMaxAttackDelay { get { return 60; } }
        public Int32 PlayerCurrentAttackDelay { get { return _ply.attackDelay;} }
        public Int32 PlayerMaxHP { get { return _ply.maxHP; } }
        public Int32 PlayerCurrentHP { get { return _ply.HP; } }
        public String PlayerHPBarText { get { return PlayerCurrentHP.ToString() + " / " + PlayerMaxHP.ToString(); } }
        public Int32 PlayerMaxFuel { get { return _ply.maxFuel; } }
        public Int32 PlayerCurrentFuel { get { return _ply.fuel; } }
        public String PlayerFuelBarText { get { return PlayerCurrentFuel.ToString() + " / " + PlayerMaxFuel.ToString(); } }
        public DelegateCommand PlayerAttackCommand { get; set; }
        public String Skill1Name { get { return _ply.getSkillName(0) + "(" + _ply.getSkillCost(0).ToString() + ")"; } }
        public DelegateCommand SkillUse { get; set; }
        public String Skill2Name { get { return _ply.getSkillName(1) + "(" + _ply.getSkillCost(1).ToString() + ")"; } }

        public String MonsterName { get { return _mob.name; } }
        public Int32 MonsterMaxAttackDelay { get { return 60; } }
        public Int32 MonsterCurrentAttackDelay { get { return _mob.attackDelay; } }
        public Int32 MonsterMaxHP { get { return _mob.maxHP; } }
        public Int32 MonsterCurrentHP { get { return _mob.HP; } }
        public DelegateCommand MonsterCommand { get; set; }
        #endregion

        public Fight_VM() { }

        public Fight_VM(PlayerCharacter ply, AbstractCreature mob)
        {
            _ply = ply;
            _mob = mob;
            InitStuff();
        }

        public Fight_VM(Model.gameModel model, AbstractCreature mob)
        {
            _model = model;
            _ply = _model.GetPlayCaharacter();
            _mob = mob;
            InitStuff();
        }

        private void InitStuff()
        {
            PlayerAttackCommand = new DelegateCommand(param =>
                                    {
                                        _ply.BasicAttack(_mob);
                                        PlayerAttackEnable = false;
                                        _model.PlayerAttackDone_Cont();
                                    });
            SkillUse = new DelegateCommand(param => 
                {
                     _ply.useSkill(Convert.ToInt32(param), _mob);
                    PlayerAttackEnable = false;
                     _model.PlayerAttackDone_Cont();
                });
            MonsterCommand = new DelegateCommand(param => _mob.BasicAttack(_ply));
            PlayerAttackEnable = false;

            _ply.HPModified += new EventHandler(RefresHPBars);
            _ply.fuelStateChanged += new EventHandler(RefreshFuelBar);
            _ply.CreatureDead += new EventHandler(PlayerDead);
            _mob.HPModified += new EventHandler(RefresHPBars);
            _mob.CreatureDead += new EventHandler(MobDead);
            _model.PlayerReadyToAttack += new EventHandler(PlayerReadyToAttack);
            _model.FightingTimerTick += new EventHandler(RefreshTimerValue);

        }

        #region event Handlers
        private void RefresHPBars(object sender, EventArgs e)
        {
            OnPropertyChanged("PlayerCurrentHP");
            OnPropertyChanged("PlayerHPBarText");
            OnPropertyChanged("MonsterCurrentHP");
        }

        private void PlayerDead(object sender, EventArgs e)
        {
            View.Switcher.Switch(new View.GameOver(_model));
            
        }

        private void RefreshFuelBar(object sender, EventArgs e)
        {
            OnPropertyChanged("PlayerCurrentFuel");
            OnPropertyChanged("PlayerFuelBarText");
        }

        private void MobDead(object sender, EventArgs e)
        {
            View.Switcher.Switch(new View.InTheTown(_model));
            _model.followTheSigns();
        }

        private void PlayerReadyToAttack(object sender, EventArgs e)
        {
            PlayerAttackEnable = true;
        }

        private void RefreshTimerValue(object sender, EventArgs e)
        {
            OnPropertyChanged("PlayerCurrentAttackDelay");
            OnPropertyChanged("MonsterCurrentAttackDelay");
        }
        #endregion
    }
}
